//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "ExchangeRate.h"
using namespace Cephei::QL;
#include <gen/QL/Money.h>
#include <gen/QL/Currency.h>
#undef HANDLE
#undef ABSTRACT
#define STRUCT
Cephei::QL::CExchangeRate::CExchangeRate (Cephei::QL::ICurrency^ source, Cephei::QL::ICurrency^ target, Double rate) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CCurrency^ _Csource;
    CCurrency^ _Ctarget;
    try
    {
#ifdef HANDLE
        _phExchangeRate = NULL;
#endif
        _Csource = safe_cast<CCurrency^> (source);
        _Csource->Lock();
        QuantLib::Currency& _source = static_cast<QuantLib::Currency&> (_Csource->GetReference ()); 
        _Ctarget = safe_cast<CCurrency^> (target);
        _Ctarget->Lock();
        QuantLib::Currency& _target = static_cast<QuantLib::Currency&> (_Ctarget->GetReference ()); 
        QuantLib::Decimal _rate = (QuantLib::Decimal)ValueHelper::Convert (rate); //d
        _ppExchangeRate = new boost::shared_ptr<QuantLib::ExchangeRate> (new QuantLib::ExchangeRate ( _source,  _target,  _rate ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Csource != nullptr) _Csource->Unlock();
        if (_Ctarget != nullptr) _Ctarget->Unlock();
    }
}
Cephei::QL::CExchangeRate::CExchangeRate () 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phExchangeRate = NULL;
#endif
        _ppExchangeRate = new boost::shared_ptr<QuantLib::ExchangeRate> (new QuantLib::ExchangeRate ( ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::CExchangeRate::CExchangeRate (boost::shared_ptr<QuantLib::ExchangeRate>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phExchangeRate = NULL;
#endif
	_ppExchangeRate = &childNative;
    
}
Cephei::QL::CExchangeRate::CExchangeRate (QuantLib::ExchangeRate& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phExchangeRate = NULL;
#endif
	_ppExchangeRate = new boost::shared_ptr<QuantLib::ExchangeRate> (&childNative);
    
    _ExchangeRateOwner = owner;
    
}

Cephei::QL::CExchangeRate::CExchangeRate (CExchangeRate^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phExchangeRate = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppExchangeRate = new boost::shared_ptr<QuantLib::ExchangeRate> (copy->GetShared());
        
    }
}
Cephei::QL::CExchangeRate::CExchangeRate (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phExchangeRate = NULL;
#endif
	if (!t->IsSubclassOf(CExchangeRate::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::CExchangeRate::CExchangeRate (QuantLib::Handle<QuantLib::ExchangeRate>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phExchangeRate = &childNative;
	_ppExchangeRate = &static_cast<boost::shared_ptr<QuantLib::ExchangeRate>>(childNative.currentLink());
    
    _ExchangeRateOwner = owner;
}
Cephei::QL::CExchangeRate::CExchangeRate (QuantLib::Handle<QuantLib::ExchangeRate> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phExchangeRate = &childNative;
	_ppExchangeRate = &static_cast<boost::shared_ptr<QuantLib::ExchangeRate>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::CExchangeRate::CExchangeRate (QuantLib::ExchangeRate childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phExchangeRate = NULL;
#endif
	_ppExchangeRate = new boost::shared_ptr<QuantLib::ExchangeRate> (new QuantLib::ExchangeRate (childNative));
    
}
#endif

Cephei::QL::CExchangeRate::~CExchangeRate ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppExchangeRate != NULL)
    {
	    delete _ppExchangeRate;
        _ppExchangeRate = NULL;
    }
}
Cephei::QL::CExchangeRate::!CExchangeRate ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppExchangeRate != NULL)
    {
	    delete _ppExchangeRate;
    }
}
QuantLib::ExchangeRate& Cephei::QL::CExchangeRate::GetReference ()
{
    if (_ppExchangeRate == NULL) throw REFNEW NativeNullException ();
	return **_ppExchangeRate;
}
boost::shared_ptr<QuantLib::ExchangeRate>& Cephei::QL::CExchangeRate::GetShared ()
{
    if (_ppExchangeRate == NULL) throw REFNEW NativeNullException ();
	return *_ppExchangeRate;
}
QuantLib::ExchangeRate* Cephei::QL::CExchangeRate::GetPointer ()
{
    if (_ppExchangeRate == NULL) throw REFNEW NativeNullException ();
	return &**_ppExchangeRate;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::ExchangeRate>& Cephei::QL::CExchangeRate::GetHandle ()
{
	if (_phExchangeRate == NULL)
	{
		_phExchangeRate = new Handle<QuantLib::ExchangeRate> (*_ppExchangeRate);
	}
	return *_phExchangeRate;
}
#endif
bool Cephei::QL::CExchangeRate::HasNative () 
{
	return (_ppExchangeRate != NULL);
}

Cephei::QL::IExchangeRate^ Cephei::QL::CExchangeRate::Chain (Cephei::QL::IExchangeRate^ r1, Cephei::QL::IExchangeRate^ r2)
{
    CExchangeRate^ _Cr1;
    CExchangeRate^ _Cr2;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cr1 = safe_cast<CExchangeRate^> (r1);
        _Cr1->Lock();
        QuantLib::ExchangeRate& _r1 = static_cast<QuantLib::ExchangeRate&> (_Cr1->GetReference ()); 
        _Cr2 = safe_cast<CExchangeRate^> (r2);
        _Cr2->Lock();
        QuantLib::ExchangeRate& _r2 = static_cast<QuantLib::ExchangeRate&> (_Cr2->GetReference ()); 
    	QuantLib::ExchangeRate _rv = (QuantLib::ExchangeRate)(*_ppExchangeRate)->chain ( _r1,  _r2 );   
        Cephei::QL::CExchangeRate^ _nrv = REFNEW Cephei::QL::CExchangeRate (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cr1 != nullptr) _Cr1->Unlock();
        if (_Cr2 != nullptr) _Cr2->Unlock();
    }
}
Cephei::QL::IMoney^ Cephei::QL::CExchangeRate::Exchange (Cephei::QL::IMoney^ amount)
{
    CMoney^ _Camount;
    try
    {
        _Camount = safe_cast<CMoney^> (amount);
        _Camount->Lock();
        QuantLib::Money& _amount = static_cast<QuantLib::Money&> (_Camount->GetReference ()); 
    	QuantLib::Money _rv = (QuantLib::Money)(*_ppExchangeRate)->exchange ( _amount );   
        Cephei::QL::CMoney^ _nrv = REFNEW Cephei::QL::CMoney (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Camount != nullptr) _Camount->Unlock();
    }
}
Double Cephei::QL::CExchangeRate::Rate::get ()
{
    try
    {
    	QuantLib::Decimal _rv = (QuantLib::Decimal)(*_ppExchangeRate)->rate ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::ICurrency^ Cephei::QL::CExchangeRate::Source::get ()
{
    try
    {
    	QuantLib::Currency& _rv = (QuantLib::Currency&)(*_ppExchangeRate)->source ( );   
        Cephei::QL::CCurrency^ _nrv = REFNEW Cephei::QL::CCurrency (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::ICurrency^ Cephei::QL::CExchangeRate::Target::get ()
{
    try
    {
    	QuantLib::Currency& _rv = (QuantLib::Currency&)(*_ppExchangeRate)->target ( );   
        Cephei::QL::CCurrency^ _nrv = REFNEW Cephei::QL::CCurrency (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
QL::ExchangeRate::TypeEnum Cephei::QL::CExchangeRate::Type::get ()
{
    try
    {
    	QuantLib::ExchangeRate::Type _rv = (QuantLib::ExchangeRate::Type)(*_ppExchangeRate)->type ( );   
        QL::ExchangeRate::TypeEnum _nrv = (QL::ExchangeRate::TypeEnum)_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::IExchangeRate^ Cephei::QL::CExchangeRate_Factory::Create (Cephei::QL::ICurrency^ source, Cephei::QL::ICurrency^ target, Double rate)
{
    return REFNEW CExchangeRate ( source,  target,  rate);
}
Cephei::QL::IExchangeRate^ Cephei::QL::CExchangeRate_Factory::Create ()
{
    return REFNEW CExchangeRate ();
}
